package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @version v1.0
 * @date 2019-7-5 14:56:01
 * @description
 **/
@Transactional
@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> roles = roleDao.findPage(queryString);
        return new PageResult(roles.getTotal(), roles.getResult());

    }
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    @Override
    public Map<String, Integer[]> findMenuIdsAndPermissionIdsByRoleId(Integer id) {

        Integer[] permissionIds = roleDao.findConnectWithPerssion(id);
        Integer[] menuIds = roleDao.findConnectWithMenu(id);
        Map<String, Integer[]> map  = new HashMap<>();
        map.put("permissionIds",permissionIds);
        map.put("menuIds",menuIds);
        return map;
    }


    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);

    }


    @Override
    public Result delete(Integer id) {
      Long count = roleDao.findConnectWithUser(id);
      if (count>0){
          return new Result(false, MessageConstant.ROLE_HAS_USEED);
      }

      roleDao.deleteConnectWithPerssion(id);
      roleDao.deleteConnectWithMenu(id);
      roleDao.deleteRole(id);

        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }


    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
                roleDao.edit(role);
        Integer id = role.getId();
        roleDao.deleteConnectWithPerssion(id);
        roleDao.deleteConnectWithMenu(id);
        setRoleConnectWithPremission(id, permissionIds);
        setRoleConnectWithMenu(id, menuIds);

    }


    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);//��������
        Integer id = role.getId();
        setRoleConnectWithPremission(id, permissionIds);
        setRoleConnectWithMenu(id, menuIds);
    }


    private void setRoleConnectWithPremission(Integer roleId, Integer[] permissionIds) {
        if(permissionIds==null || permissionIds.length<1){
            return;
        }
        List<Map> list = new ArrayList<>();
        for (Integer permissionId : permissionIds) {
            Map<Object, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("permissionId", permissionId);
            list.add(map);
        }
        roleDao.addConnectWithPremission(list);
    }


    private void setRoleConnectWithMenu(Integer roleId, Integer[] menuIds) {
        if(menuIds==null || menuIds.length<1){
            return;
        }
        List<Map> list = new ArrayList<>();
        for (Integer menuId : menuIds) {
            Map<Object, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("menuId", menuId);
            list.add(map);
        }
        roleDao.addRoleConnectWithMenu(list);

    }
}
